package cn.gx.demo.controller;

import cn.gx.kevin.common.annotation.ApiDescript;
import cn.gx.kevin.common.annotation.ApiParams;
import cn.gx.kevin.common.ui.AdminControllerHelper;
import cn.gx.kevin.common.utils.SysConfigParams;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@ApiDescript("测试功能")
@Controller
@RequestMapping(value = "/test")
public class UploadTestController {

    Logger logger = org.slf4j.LoggerFactory.getLogger(UploadTestController.class);

    @Autowired
    private Environment environment;

    @RequestMapping(value = "/page/{pageName}", method = RequestMethod.GET)
    public void page(@PathVariable("pageName") String pageName, HttpServletRequest request) throws Exception {

    }


    @ApiDescript("上传")
    @ApiParams("{}")
    @RequestMapping("upload")
    public void importXls(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "attachments", required = false) MultipartFile[] uploadFiles) throws Exception {

        String uploadDir = SysConfigParams.getUploadFilesDir();
        String fileName = "";
        String p = request.getParameter("p");
        if (uploadFiles != null) {
            for (MultipartFile file : uploadFiles) {
                fileName = file.getOriginalFilename();
                file.transferTo(new File(uploadDir + file.getOriginalFilename()));
                System.out.println(file.getOriginalFilename());
            }
        }
        Map<String, Object> data = new HashMap<>();
        data.put("name", "kevin");
        data.put("url", "/upload/images/" + fileName);
        AdminControllerHelper.responseJson(response, 0, "数据已经上传成功!", data);
    }

    @ApiDescript("上传")
    @ApiParams("{}")
    @RequestMapping("uploadImg")
    public void uploadImg(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "attachments", required = false) MultipartFile[] uploadFiles) throws Exception {

        String uploadDir = SysConfigParams.getUploadImagesDir() ;
        String fileName = "";
        Map<String, Object> data = new HashMap<>();
        try{
            if (uploadFiles != null) {
                for (MultipartFile file : uploadFiles) {
                    fileName = file.getOriginalFilename();
                    File fileImg = new File(uploadDir + fileName);
                    file.transferTo(fileImg);
                    InputStream inputStream = new FileInputStream(fileImg);
                    BufferedImage sourceImg = ImageIO.read(inputStream);
                    data.put("width",sourceImg.getWidth());
                    data.put("height",sourceImg.getHeight());
                    inputStream.close();
                }
            }
        }catch (Exception x){
            logger.error("",x);
            logger.info(uploadDir);
        }

        data.put("url", "/upload/images/" + fileName);
        AdminControllerHelper.responseJson(response, 0, "数据已经上传成功!", data);

    }

}
